package com.bs.rss.common.interp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bs.rss.common.model.data.User;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation ai) {
		User user = ai.getController().getSessionAttr("currentuser");
		  //判断是否ajax请求
	    String header = ai.getController().getRequest().getHeader("X-Requested-With");
	    HttpServletRequest request = ai.getController().getRequest();
	    HttpServletResponse response = ai.getController().getResponse();
	    boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header);
	    
		if (user == null) {
			if(isAjax){
				
				response.setHeader("sessionstatus", "00004");
				response.setHeader("location", request.getContextPath()+"");
	              
				return;
			}else{
				
			ai.getController().redirect("/");
			
			}
		} else {
			
		    String type="";
		    if(isAjax){type="AJAX";}
			System.out.println("----------------------------------"+type+"------------------------------------------");
			System.out.println("ActionKey   :"+ai.getActionKey());
			System.out.println("ViewPath    s:"+ai.getViewPath());
			System.out.println("--------------------------------------------------------------------------------");
			
			ai.invoke();

		
		}
	}




}
